define(["layuiall","zTree","jquery","jq_request","jqform"],function (layui,zTree,$,jq_request,jqform){
    var Controller ={
        index:function () {
            var form = layui.form;
            form.on('switch(auth)', function(data){
                var auth = 0;
                if(data.elem.checked){
                    auth = 1
                }
                ajxAddmenu({
                    id:data.value,
                    auth:auth
                },$(data.elem).attr("data-url"));
            });
            $(".ref-node").click(function () {
                layer.open({
                    skin:'refNode_iframe',
                    title:'菜单刷新',
                    type:2,
                    content:$(this).attr("data-url"),
                    area:["650px","450px"],
                    offset: '100px',
                    shadeClose:true
                })
            })
            $(".clearExceptionNode").click(function () {
                jq_request.get($(this).attr("data-url"),[])
            })
            jqform.delete()

            //数据全部选中
            form.on('checkbox(select-all)', function(data){

                if(data.elem.checked==true){
                    $("input[name='idList[]']").prop('checked',true)
                }else {
                    $("input[name='idList[]']").prop('checked',false)
                }
                form.render('checkbox');
            });
        },
        refnode:function () {
            $(".back").click(function () {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            })
        },
        addapi:function () {
            level()
            jqform.init()
        },
        editapi:function () {
            level()
            jqform.init()
        },

    };

    //层级选择
    function level() {
        $(".apicate").click(function () {
            var TreeHtml = '<ul id="MenuTree" class="ztree"></ul>';
            var menuTreeAler = layer.open({
                type: 1,
                title: "api栏目选择",
                content: TreeHtml,
                area:["300px","350px"],
                shadeClose:true,
                success: function () {
                    jq_request.post($(this).attr("data-url"), {
                        data: {
                            datasType: 'tree'
                        },
                        success: function (res) {
                            var zTreeObj;
                            var setting = {
                                view: {
                                    showIcon: true
                                },
                                data: {
                                    simpleData: {
                                        enable: true,
                                        idKey:"id",
                                        pIdKey:"parent_id",
                                    },
                                    key:{
                                        name:"title"
                                    }
                                },
                                check: {
                                    enable: true,
                                    chkStyle: "radio",
                                    radioType: "all"
                                },
                                callback: {
                                    onClick: function (e, treeId, treeNode, clickFlag) {
                                        zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                        nodes = zTreeObj.getCheckedNodes(true),
                                            v = "";
                                        for (var i = 0; i < nodes.length; i++) {
                                            v += nodes[i].id
                                            $("input[name='parent_id']").val(v).siblings("input").val( nodes[i].title)
                                        }
                                        layer.close(menuTreeAler)
                                    },

                                    onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                        $("input[name='parent_id']").val(treeNode.id).siblings("input").val(treeNode.title)
                                        layer.close(menuTreeAler)
                                    },
                                }
                            };
                            zTreeObj = $.fn.zTree.init($("#MenuTree"), setting, res);
                            zTreeObj.expandAll(true);
                        }
                    })
                }
            })
        })
    }

    //提交菜单
    function ajxAddmenu(data,url) {
        $.ajax({
            type:'post',
            url:url,
            data:data,
            success:function (res) {
                layer.msg(res.msg)
            }
        })
    }

    return Controller;
});